package com.clp.protocol.iec104.iec104.client.pipeline.handler;

import com.clp.protocol.iec104.iec104.apdu.Apdu;
import com.clp.protocol.iec104.iec104.client.Master;
import com.clp.protocol.iec104.iec104.client.pipeline.DuplexMasterHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class STypeMasterHandler extends DuplexMasterHandler {
    public STypeMasterHandler(Master master) {
        super(master);
    }

    @Override
    public void whenActive(ChannelHandlerContext ctx) throws Exception {

    }

    @Override
    public void whenRecvingApdu(ChannelHandlerContext ctx, Apdu apdu) {
        if (apdu.isSType()) { // 如果是S帧，则进行处理
            // 接收到从站发送的确认帧
            log.debug("S: 收到S帧\n{}", apdu);
        }

        passRecvingApdu(ctx, apdu);
    }

    @Override
    public void whenSendingApdu(ChannelHandlerContext ctx, Apdu apdu, ChannelPromise promise) {
        if (apdu.isSType()) { // 如果是S帧，则进行处理
            // 接收到从站发送的确认帧
            log.debug("S: 发送S帧\n{}", apdu);
        }

        passSendingApdu(ctx, apdu, promise);
    }

    @Override
    public void whenInActive(ChannelHandlerContext ctx) throws Exception {

    }

    @Override
    public void whenExceptionCaught(ChannelHandlerContext ctx, Throwable cause) {

    }
}
